package me.zhengjie.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis-Plus 备用配置
 * 如果主配置出现问题，可以使用这个配置替代
 */
// @Configuration
public class MybatisPlusConfigFallback {

    /**
     * 使用最基本的配置方式
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        // 使用反射创建分页插件，避免导入问题
        try {
            Class<?> paginationClass = Class.forName("com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor");
            Object paginationInterceptor = paginationClass.getConstructor(DbType.class).newInstance(DbType.MYSQL);
            
            // 设置最大单页限制数量
            paginationClass.getMethod("setMaxLimit", Long.class).invoke(paginationInterceptor, 500L);
            
            interceptor.addInnerInterceptor((com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor) paginationInterceptor);
        } catch (Exception e) {
            System.err.println("Failed to configure pagination interceptor: " + e.getMessage());
        }
        
        return interceptor;
    }

    /**
     * 全局配置
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);
        return globalConfig;
    }
}